//
//  ImageView.m
//  NVAReader
//
//  Created by DuyLK on 6/9/14.
//  Copyright (c) 2014 com.nva.nvareader. All rights reserved.
//

#import "ImageView.h"

@implementation ImageView
-(id) initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if(self)
    {
        //set boder color
        NSRect rect = NSMakeRect(0, 0, 260 , 430);
        image = [[NSImageView alloc] initWithFrame:rect];
        [image setImageScaling:NSScaleToFit];
        [image setImage:[NSImage imageNamed:@"picture.png"]];
        [self addSubview:image];
    }
    return self;
}

-(void) setImageView:(NSImage*)imageData{
    //remove all view
    for (int i=(int)self.subviews.count - 1; i>=0; i--)
        [[self.subviews objectAtIndex:i] removeFromSuperview];
    //add new view
    NSRect rect = NSMakeRect(0, 0, self.frame.size.width , self.frame.size.height );
    image =[[NSImageView alloc] initWithFrame:rect];
    [image setImageScaling:NSScaleToFit];
    [image setImage:imageData];
    [self addSubview:image];
}

-(void) drawRect:(NSRect)dirtyRect
{
    [NSGraphicsContext saveGraphicsState];
    
    // Create the shadow below and to the right of the shape.
    NSShadow* theShadow = [[NSShadow alloc] init];
    [theShadow setShadowOffset:NSMakeSize(5.0, -5.0)];
    [theShadow setShadowBlurRadius:2.0];
    
    // Use a partially transparent color for shapes that overlap.
    [theShadow setShadowColor:[[NSColor blackColor]
                               colorWithAlphaComponent:0.3]];
    [theShadow set];
    
    // Draw your custom content here. Anything you draw automatically has the shadow effect applied to it.
    NSRect size = NSMakeRect(0,10, 40, 40);
    NSBezierPath *bPath = [NSBezierPath bezierPathWithRect:size];
    [[NSColor whiteColor] set];
    [bPath fill];
    [NSGraphicsContext restoreGraphicsState];
   // [self drawBorder:dirtyRect];
}

-(void)drawBorder:(NSRect)rect{
    //  NSRect rect = [self bounds];
    NSRect frameRect = [self bounds];
    
    if(rect.size.height < frameRect.size.height)
        return;
    NSRect newRect = NSMakeRect(rect.origin.x+2, rect.origin.y+2, rect.size.width-3, rect.size.height-3);
    
    NSBezierPath *textViewSurround = [NSBezierPath bezierPathWithRoundedRect:newRect xRadius:0 yRadius:0];
    [textViewSurround setLineWidth:2];
    [textViewSurround stroke];
}

@end
